﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using RpgLibrary.WorldClasses;
using RpgLibrary;

namespace XLevelEditor
{
    public partial class FormNewTileset : Form
    {
        #region Field Region

        //bool okPressed;
        TilesetData tilesetData;

        #endregion

        #region Property Region

        public TilesetData TilesetData
        {
            get { return tilesetData; }
        }

        private string PassabilityImageName
        {
            get
            {
                return Program.NormalizeAssetName(tbTilesetImage.Text) + "-pass";
            }
        }
        
        #endregion

        #region Constructor Region

        public FormNewTileset()
        {
            InitializeComponent();

            btnSelectImage.Click += new EventHandler(btnSelectImage_Click);
            btnOK.Click += new EventHandler(btnOK_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);

            tbTilesetName.Text = "Village Tileset";
        }

        #endregion

        #region Button Event Handler Region

        void btnSelectImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofDialog = new OpenFileDialog();

            ofDialog.Filter = "Image Files|*.BMP;*.GIF;*.JPG;*.TGA;*.PNG";
            ofDialog.CheckFileExists = true;
            ofDialog.CheckPathExists = true;
            ofDialog.Multiselect = false;
            if (!string.IsNullOrEmpty(Program.PreviouslyLoadedGamePath))
            {
                ofDialog.InitialDirectory = Path.Combine(
                    Path.GetDirectoryName(
                        Path.GetDirectoryName(
                            Path.GetDirectoryName(Program.PreviouslyLoadedGamePath))),
                    "Tilesets");
            }
            DialogResult result = ofDialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                tbTilesetImage.Text = ofDialog.FileName;
            }
        }

        void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbTilesetName.Text))
            {
                MessageBox.Show("You must enter a name for the tileset.");
                return;
            }

            if (string.IsNullOrEmpty(tbTilesetImage.Text))
            {
                MessageBox.Show("You must select an image for the tileset.");
                return;
            }

            //this is the same call I make in FormMain right after this method ends. It's not efficient to call it 
            //twice - fix at some point
            System.Drawing.Image image = (System.Drawing.Image)System.Drawing.Bitmap.FromFile(tbTilesetImage.Text);

            tilesetData = new TilesetData();
            tilesetData.TilesetName = tbTilesetName.Text;
            tilesetData.TilesetImageName = Program.NormalizeAssetName(tbTilesetImage.Text);
            tilesetData.TilesetPassabilityImageName = PassabilityImageName;
            tilesetData.TileWidthInPixels = RpgConstants.TileWidth;
            tilesetData.TileHeightInPixels = RpgConstants.TileWidth;
            tilesetData.TilesWide = image.Width / RpgConstants.TileWidth;
            tilesetData.TilesHigh = image.Height / RpgConstants.TileHeight;

            //okPressed = true;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            //okPressed = false;
            this.Close();
        }

        #endregion
    }
}